package ClientUtils

import (
	"context"
	"github.com/go-redis/redis/v8"
	ClientInitial "goGrpc/allClient/Initial"
	"log"
	"time"
)

func RedisAuth(key string) (idAuth string, errAuth string) {
	errAuth = ""
	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
	defer cancel()
	pong, err := ClientInitial.RedisConnection.Ping(ctx).Result()
	if err != nil {
		log.Printf("redisAuth连接池工作异常！err:%v\n", err)
		errAuth = "异常请联系管理员！"
	}
	log.Println("redisAuth链接状态", pong, err)
	val, err := ClientInitial.RedisConnection.Get(ctx, key).Result()
	if err == redis.Nil {
		log.Printf("redisAuth值已经失效，失效查询value: %v \n", key)
		errAuth = "登入失效，请重新登入"
	} else if err != nil {
		log.Printf("redisAuth异常: %v \n", err)
		errAuth = "异常请联系管理员！"
	} else {
		log.Println("redisAuth查询结果value:", val)
	}
	return val, errAuth
}
